##### R CODE TO REPLICATE FIGURE 1 IN 
##### Adrian Lucardi, "Building Support from Below? Subnational Elections, Diffusion Effects, and the Growth of the Opposition in Mexico, 1984-2000," Comparative Political Studies, 49(14), 2016, 1855-1895.

## Loading the required R packages. Make sure to have them installed before proceeding. In order to install a package called X, write install.packages("X")
library (car)
library (classInt)
library (foreign)
library (ggplot2)
library (plm)
library (plyr)
library (RColorBrewer)
library (rgdal)
library (shapefiles)
library (spdep)
library (xtable)

## display options
options (digits=4, scipen=6, show.signif.stars=FALSE)

## working directory -> change this to the directory where your have your data
setwd ("/Users/adrianlucardi/Desktop/Replication/")

###### DOWNLOADING THE DATA
baseFull <- read.dta ("Mexico Municipal data 1984-2000.dta")
summary (baseFull); dim (baseFull)


### Replicating Figure 1

## The original dataset is at the municipality level. We have to collapse it at the state-year level:
baseFull$counter <- 1
states <- as.data.frame (with (baseFull, names (by (counter, stateYear, sum, na.rm=TRUE))))
colnames (states) <- "stateYear"
states$state <- with (states, factor (unlist (strsplit (as.character (stateYear), split="_"))[seq (1, 2*nrow (states), by=2)]))
states$year <- with (states, as.numeric (unlist (strsplit (as.character (stateYear), split="_"))[seq (2, 2*nrow (states), by=2)]))
states$n.munis <- with (baseFull, by (counter, stateYear, sum, na.rm=TRUE)[])
states$population <- with (baseFull, by (population, stateYear, sum, na.rm=TRUE)[])

states$incPRI <- with (baseFull, by (incPRI, stateYear, sum, na.rm=TRUE)[])
states$incOpp <- with (baseFull, by ((incPAN + incPRD + incPAN_PRD + incOTH), stateYear, sum, na.rm=TRUE)[])
states$incPAN <- with (baseFull, by (incPAN, stateYear, sum, na.rm=TRUE)[])
states$incPRD <- with (baseFull, by (incPRD, stateYear, sum, na.rm=TRUE)[])
states$incPRD <- with (states, ifelse (year < 1989, NA, incPRD))
states$incPAN_PRD <- with (baseFull, by (incPAN_PRD, stateYear, sum, na.rm=TRUE)[])

states$shIncPRI <- with (states, incPRI/n.munis)
states$shIncOpp <- with (states, incOpp/n.munis)
states$shIncPAN <- with (states, incPAN/n.munis)
states$shIncPRD <- with (states, incPRD/n.munis)
states$shIncPAN_PRD <- with (states, incPAN_PRD/n.munis)
summary (states); dim (states)

## creating the year averages
states2 <- with (states, as.data.frame (names (by (n.munis, year, sum))))
colnames (states2) <- "year"
states2$year <- as.numeric (as.character (states2$year))
states2$n.munis <- with (states, by (n.munis, year, sum, na.rm=T)[])
states2$incPAN <- with (states, by (incPAN, year, sum, na.rm=T)[])
states2$incPRI <- with (states, by (incPRI, year, sum, na.rm=T)[])
states2$incPRD <- with (states, by (incPRD, year, sum, na.rm=T)[])
states2$incPRD <- with (states2, ifelse (year < 1989, NA, incPRD))
states2$shIncPAN <- with (states2, incPAN / n.munis)
states2$shIncPRI <- with (states2, incPRI / n.munis)
states2$shIncPRD <- with (states2, incPRD / n.munis)



### Building the plots

## setting the formatting values
cex.light <- 0.6
cex.dark <- cex.light*5
cex.text <- 18
lim.x <- c (1984,2000)
lim.y <- c (0,1)

lightPRI <- "forestgreen"
darkPRI <- "darkgreen"
lightPAN <- "dodgerblue"
darkPAN <- "dodgerblue4"
lightPRD <- "goldenrod3"
darkPRD <- "goldenrod4"

(aspect.ratio <- 2/1)
plotWidth <- 720

# Figure 1a
png ("Figures/evolution_winPRI.png", w=plotWidth, h=plotWidth/aspect.ratio)
ggplot (data=states, aes (x=year, y=shIncPRI, group=state)) +
  geom_line (colour=lightPRI, size=cex.light) +
  geom_line (data=states2, aes (x=year, y=shIncPRI, group=FALSE), colour=darkPRI, size=cex.dark) +
  xlim (lim.x) +
  ylim (lim.y) +
  xlab ("") +
  ylab ("Proportion of municipalities\ngoverned by the PRI") +
  theme_bw (base_size=cex.text)
dev.off ()  ## warnings reported. Don't worry

# Figure 1b
png ("Figures/evolution_winPAN.png", w=plotWidth, h=plotWidth/aspect.ratio)
ggplot (data=states, aes (x=year, y=shIncPAN, group=state)) +
  geom_line (colour=lightPAN, size=cex.light) +
  geom_line (data=states2, aes (x=year, y=shIncPAN, group=FALSE), colour=darkPAN, size=cex.dark) +
  xlim (lim.x) +
  ylim (lim.y) +
  xlab ("") +
  ylab ("Proportion of municipalities\ngoverned by the PAN") +
  theme_bw (base_size=cex.text)
dev.off ()

# Figure 1c
png ("Figures/evolution_winPRD.png", w=plotWidth, h=plotWidth/aspect.ratio)
ggplot (data=states, aes (x=year, y=shIncPRD, group=state)) +
  geom_line (colour=lightPRD, size=cex.light) +
  geom_line (data=states2, aes (x=year, y=shIncPRD, group=FALSE), colour=darkPRD, size=cex.dark) +
  xlim (lim.x) +
  ylim (lim.y) +
  xlab ("") +
  ylab ("Proportion of municipalities\ngoverned by the PRD") +
  theme_bw (base_size=cex.text)
dev.off ()
